
/* Generated by Interface Builder */

#import "CookieController.h"
#import "PreferencesController.h"
#import <appkit/appkit.h>
#import <appkit/Font.h>
#import "CookieFile.h"
#import <defaults.h>
#import <libc.h>


#define MINWIDTH	200.0
#define MINHEIGHT	150.0

NXDefaultsVector mydefaults = {
	{ "Status","ALIVE" },
	{ "Timer","NO" },
	{ "Seconds", "60" },
	{ "MainFrame","175 555 533 220" },
	{ "NXFont", "Ohlfs" },
	{ "NXFontSize","10.0" },
	{ NULL } };

void RunCookieEntry(DPSTimedEntry te, double timeNow, void *data)
{
  /* we set data to self so we can call this method from the timed entry */
	[(CookieController *)data timedEntry];
}

@implementation CookieController

- windowWillClose:sender
{
	[NXApp terminate:self];

	return self;
}

- windowWillResize:window toSize:(NXSize *)size
{
	if (size->width  < MINWIDTH)  size->width = MINWIDTH;
	if (size->height < MINHEIGHT) size->height = MINHEIGHT;

	return self;
}

- timedEntry
{
	if([prefs flags]&PREF_DEAD)
		[NXApp terminate:self];

	if([prefs flags]&PREF_TIMER)
		[self newCookie:self];

	return self;
}

- newCookie:sender
{
	int count,cookieCount = [((List *)cookieList) count];
	CookieFile *cookieFile;
	Cookie *cookie;
	char text[2048];
	int length = 0,random_size;

	// First of all check to see if there are any cookies
	if(!cookieCount)
	{
		NXRunAlertPanel("Fatal Application Error",
		 "There are no cookies to display!","OK!",NULL,NULL);
		 [NXApp terminate:self];
	}

	// The have a look see if there are any selected
	for(count = 0; count < cookieCount; count++)
	{
		cookieFile = [cookieList objectAt:count];
	
		if([cookieFile selected])
			length += [cookieFile filelength];
	}

	if(!length)
	{
		NXRunAlertPanel("Application Error",
			"There are no cookies selected to display, please select some","OK!",
			NULL,NULL);
		[prefs startPreferences:self];
		return self;
	}

	random_size = random()%length;

	for(count = 0,length = 0; count < cookieCount; count++)
	{
		cookieFile = [cookieList objectAt:count];

		if(![cookieFile selected])
			continue;

		if((random_size >= length) &&
		   (random_size <= (length+[cookieFile filelength])))
			break;

		length += [cookieFile filelength];
	}

	cookieFile = [cookieList objectAt:count];

	cookie = [cookieFile pickRandom];

	[[titleText setStringValue:[cookie title]] display];

	text[0] = 0;
	for(count = 0; count < [[cookie cookieLines] count]; count++)
	{
		strcat(text,[[[cookie cookieLines] objectAt:count] string]);
		strcat(text,"\n");
	}

	[[bodyText docView] setText:text];

	return self;
}

- appDidInit:sender
{
	flags.entry_running = FALSE;
	srandom(time(0));

	NXRegisterDefaults([NXApp appName], mydefaults);

	[prefs getPreferences];
	[self setWindowPrefs];

	[self newCookie:self];

	if(!strcmp(NXGetDefaultValue([NXApp appName],"NXAutoLaunch"),"YES"))
		[cookieWindow setFloatingPanel:TRUE];

	[cookieWindow makeKeyAndOrderFront:self];

	return self;
}

- setWindowPrefs
{
	Font *font;
	NXRect myRect;

	font = [Font newFont:[prefs fontname] size:[((PreferencesController *)prefs) fontsize]];
	[[bodyText docView] setFont:font];

	myRect = [prefs windowframe];
	[cookieWindow placeWindowAndDisplay:&myRect];

	return self;
}

- newPrefs
{
	if(flags.entry_running)
	{
		DPSRemoveTimedEntry(myTimedEntry);
		flags.entry_running = FALSE;
	}

	if(([prefs flags]&PREF_DEAD) || ([prefs flags]&PREF_TIMER))
	{
		myTimedEntry =
			DPSAddTimedEntry((float)[prefs seconds],
				&RunCookieEntry,self,NX_BASETHRESHOLD);
		flags.entry_running = TRUE;
	}

	return self;
}

- (float)fontsize
{
	return [[[bodyText docView] font] pointSize];
}

- (const char *)fontname
{
	return [[[bodyText docView] font] name];
}

- (NXRect)getWindowRect
{
	NXRect myRect;

	[cookieWindow getFrame:&myRect];

	return myRect;
}

@end
